home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / z150_src.lzh / MISC2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-12  |  6.6 KB  |  270 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) misc2.c 1.8 87/05/29 12:54:33";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. */
  8. #include "options.h"
  9. /* Miscellaneous routines needed by both Zoo and Ooz */
  10. #ifdef NOFCNTL
  11. #include <file.h>
  12. #else
  13. #include <fcntl.h>
  14. #endif
  15.  
  16. #ifdef FLAT
  17. #include <types.h>
  18. #include <stat.h>
  19. #else
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #endif
  23.  
  24. #include "portable.h"
  25. #include <stdio.h>
  26. #include "various.h"
  27. #include "zoofns.h"     /* only for malloc */
  28. #include "errors.i"
  29. #include "zoomem.h"
  30. #include "zoo.h"
  31.  
  32. /**********************/
  33. /* memerr() */
  34. /* Give error message on memory error and abort */
  35. void memerr()
  36. {
  37. #ifdef OOZ
  38.    prterror ('f', no_memory, "", "");
  39. #else
  40.    prterror ('f', no_memory);
  41. #endif
  42. }
  43.  
  44. /**********************/
  45. /*
  46. emalloc() allocates memory like malloc() does, except that it automatically
  47. calls the error function memerr() if memory couldn't be allocated.  It also
  48. assumes (unless small memory allocation is being done) that memory will
  49. never be freed and conserves it by allocating memory in large chunks
  50. and then partitioning it out with no administrative overhead.
  51. */
  52.  
  53. char *emalloc (size)
  54. unsigned int size;
  55. {
  56. #define  BLOCK_SIZE  512      /* memory allocation granularity */
  57.  
  58. #ifdef USE_MALLOC
  59. /* Pass on memory requests to malloc() */
  60.    char *ptr;
  61.    if ((ptr = malloc (size)) == NULL)
  62.       memerr();
  63.    return (ptr);
  64. #else
  65.    static char *memptr;
  66.    static unsigned avail = 0;
  67.    unsigned malloc_incr;
  68.    char *retval;
  69.  
  70.    if (size == 0)
  71.       return (NULL);
  72.  
  73.    /* if not enough space avail get some more */
  74.    if (avail < size) {
  75.       malloc_incr = BLOCK_SIZE;
  76.       if (malloc_incr < size)
  77.          malloc_incr = size;
  78.       while (malloc_incr >= size && (memptr = malloc (malloc_incr)) == NULL)
  79.          malloc_incr = (malloc_incr / 6) * 5;
  80.       avail = malloc_incr;
  81.    }
  82.  
  83.    if (avail < size)
  84.       memerr();
  85.    else {
  86.       retval = memptr;
  87.       memptr += size;
  88.       avail -= size;
  89.       return (retval);
  90.    }
  91. #endif  /* end of not USE_MALLOC */
  92. }
  93.  
  94. /**********************/
  95. /* putstr()
  96. This function prints a string to standard output without using printf().
  97. If a null string, nothing is printed (not even the null character).
  98. */
  99. putstr (str)
  100. register char *str;
  101. {
  102.    if (str == NULL)
  103.       return;
  104.    while (*str) {
  105.       fputchar (*str++);
  106.    }
  107. }
  108.  
  109. /**********************/
  110. /* exists()
  111. This function checks the existence of a file.  
  112.  
  113. If the symbol EXISTS is defined, that is called as a macro and
  114. supplied the filename.  It must return 1 if the file exists and
  115. 0 if it does not.
  116.  
  117. If EXISTS is not defined, exists() tests to see if the file can be 
  118. opened for either reading or writing;  if so, it returns 1 else it 
  119. returns 0. 
  120.  
  121. Because of the delay between the time existence is checked and the time Zoo
  122. creates a files, a race condition exists.  It would be better to
  123. use open() with the O_EXCL flag but that will not work for many
  124. systems.
  125. */
  126.  
  127. int exists (fname)
  128. char *fname;
  129. {
  130. #ifdef EXISTS
  131.     return EXISTS(fname);
  132. #else
  133.    int han;
  134.  
  135. #ifdef PORTABLE
  136.    if ((han = OPEN(fname,F_READ)) != -1 || (han = OPEN(fname,F_WRITE)) != -1)
  137. #else
  138.    /* Under MS-DOS, all files are readable */
  139.    if ((han = OPEN(fname,F_READ)) != -1)
  140. #endif /* ifdef PORTABLE */
  141.     {
  142.       close (han);
  143.       return (1);
  144.    } else
  145.       return (0);
  146. #endif /* ifdef EXISTS */
  147. }
  148.  
  149. /************************************************************************
  150. The following MS-DOS-specific functions read directory entries and zoo
  151. headers.  Portable versions are elsewhere.
  152.  
  153. Note:  At some future time, it will be better to make the MS-DOS versions
  154. of these into macros.
  155. */
  156.  
  157. /* Currently using portable I/O for MSC too */
  158. #ifdef COMMENT
  159. #ifndef PORTABLE
  160. /**********************
  161. Function frd_zooh() reads the header of a Zoo archive from a FILE.
  162. */
  163. int frd_zooh(zoo_header, zoo_file)
  164. struct zoo_header *zoo_header;
  165. FILE *zoo_file;
  166. {
  167.    if (fread ((char *) zoo_header, SIZ_ZOOH, 1, zoo_file) != 1)
  168.       return (-1);
  169.    else
  170.       return (0);
  171. }
  172.  
  173. /**********************
  174. Function frd_dir() reads a directory entry from a FILE.
  175. */
  176. int frd_dir(direntry, zoo_file)
  177. struct direntry *direntry;
  178. FILE *zoo_file;
  179. {
  180.    if (fread ((char *) direntry, SIZ_DIR, 1, zoo_file) != 1)
  181.       return (-1);
  182.    else
  183.       return (0);
  184. }
  185.  
  186. /**********************
  187. Function rd_zooh() reads a Zoo archive header from a file handle.
  188. */
  189. int rd_zooh (header, zoo_han)
  190. struct zoo_header *header;
  191. int zoo_han;
  192. {
  193.    return (read (zoo_han, (char *) header, SIZ_ZOOH));
  194. }
  195.  
  196. /**********************
  197. Function rd_dir() reads a directory entry from a file handle */
  198. int rd_dir(direntry, zoo_han)
  199. struct direntry *direntry;
  200. int zoo_han;
  201. {
  202.    return (read (zoo_han, (char *) direntry, SIZ_DIR));
  203. }
  204.  
  205. #endif /* end of not PORTABLE */
  206. #endif /* COMMENT */
  207.  
  208. /****************
  209. newcat() allocates enough space to concatenate two strings then returns
  210. a pointer to the concatenated result */
  211.  
  212. char *newcat (r, s)
  213. char *r, *s;
  214. {
  215.    char *temp = emalloc (strlen (r) + strlen (s) + 2); /* 1 spare */
  216.    strcpy (temp, r);
  217.    strcat (temp, s);
  218.    return (temp);
  219. }
  220.  
  221.  
  222. /* Creates a path */
  223. int makepath(path)
  224. char *path;
  225. {
  226.    char tmppath[PATHSIZE];
  227.    char *slashpos;
  228.    if (path == NULL)
  229.       return;
  230.    while (*lastptr(path) == *PATH_CH)     /* remove trailing slashes */
  231.       *lastptr(path) = '\0';
  232.    if (*path == '\0')
  233.       return;
  234.  
  235.    slashpos = findlast(path, PATH_CH);    /* find last slash */
  236.    if (slashpos == NULL) {                /* if not, just create dir. */
  237.       MKDIR(path);
  238.       return;
  239.    } else {                               /* otherwise...         */
  240.       if (slashpos == path) {             /* if leading slash */
  241.          MKDIR(slashpos);                 /* make that directory */
  242.          return;                          /* and done */
  243.       } else {
  244.          strcpy(tmppath,path);            /* save path */
  245.          *slashpos = '\0';                /* split into prefix & suffix */
  246. #ifdef DEBUG
  247.          printf("making path from [%s]\n", path);
  248. #endif
  249.          makepath(path);                     /* make path from prefix */
  250. #ifdef DEBUG
  251.          printf("making dir from [%s]\n", tmppath);
  252. #endif
  253.          MKDIR(tmppath);                  /* make dir from suffix */
  254.       }
  255.    }
  256. } /* makepath() */
  257.  
  258. /*
  259. If no extension in filename add supplied extension
  260. */
  261. char *addext (fname, ext)
  262. char *fname;
  263. char *ext;
  264. {
  265.    if (strchr (nameptr (fname), EXT_CH) == NULL)
  266.       return (newcat (fname, ext));
  267.    else
  268.       return (fname);
  269. }
  270.